package com.warden.myapplication.Fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.leon.lib.settingview.LSettingItem;
import com.warden.myapplication.Activity.AboutActivity;
import com.warden.myapplication.Activity.ColloctionActivity;
import com.warden.myapplication.Activity.ElectricTicketActivity;
import com.warden.myapplication.Activity.FeedBackActivity;
import com.warden.myapplication.Activity.ListActivity;
import com.warden.myapplication.Activity.LoginActivity;
import com.warden.myapplication.Activity.OrderActivity;
import com.warden.myapplication.Activity.PanssegerInformationActivity;
import com.warden.myapplication.Activity.SettingsActivity;
import com.warden.myapplication.R;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link FourthFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link FourthFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FourthFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;
    private Context context;

    public FourthFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FourtFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static FourthFragment newInstance(String param1, String param2) {
        FourthFragment fragment = new FourthFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        context = getContext();
        View view = inflater.inflate(R.layout.fragment_fourt, container, false);
        initView(view);
        return view;
    }
    private void initView(final View view){
        ImageView avaterBackground = (ImageView) view.findViewById(R.id.avatar_background) ;
        Glide.with(getContext()).load(R.drawable.user_background).centerCrop().into(avaterBackground);
        Button buttonLogin=(Button) view.findViewById(R.id.login);
        LSettingItem lSettingItemMeassage = (LSettingItem) view.findViewById(R.id.item_message);
        LSettingItem lSettingItemAbout = (LSettingItem) view.findViewById(R.id.item_about);
        LSettingItem lSettingItemSetting = (LSettingItem)view.findViewById(R.id.item_setting);
        LSettingItem lSettingItemHistoryUserInfor = (LSettingItem)view.findViewById(R.id.item_history_user_information);
        LSettingItem lSettingItemTicket = (LSettingItem) view.findViewById(R.id.item_ticket);
        LSettingItem lSettingItemColloction = (LSettingItem)view.findViewById(R.id.item_collocation);
        LSettingItem lSettingItemOrder = (LSettingItem)view.findViewById(R.id.item_order);
        LSettingItem lSettingItemfeedback=(LSettingItem)view.findViewById(R.id.item_feedback);
        lSettingItemSetting.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
            @Override
            public void click() {
                context = getContext();
                Intent intent = new Intent(context, SettingsActivity.class);
                context.startActivity(intent);
            }
        });
        lSettingItemMeassage.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
            @Override
            public void click() {
                Intent intent = new Intent(context, ListActivity.class);
                context.startActivity(intent);
                Toast.makeText(getContext().getApplicationContext(), "我的消息", Toast.LENGTH_SHORT).show();
            }
        });

        lSettingItemOrder.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
            @Override
            public void click() {
                Intent intent = new Intent(context, OrderActivity.class);
                context.startActivity(intent);
            }
        });
        lSettingItemColloction.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
            @Override
            public void click() {
                Intent intent = new Intent(context, PanssegerInformationActivity.class);
                context.startActivity(intent);

            }
        });
        lSettingItemTicket.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
            @Override
            public void click() {
                Intent intent = new Intent(context, ElectricTicketActivity.class);
                context.startActivity(intent);
            }
        });
        lSettingItemColloction.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
            @Override
            public void click() {
                Intent intent = new Intent(context, ColloctionActivity.class);
                context.startActivity(intent);
            }
        });
        lSettingItemfeedback.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
            @Override
            public void click() {
                Intent intent = new Intent(context, FeedBackActivity.class);
                context.startActivity(intent);
            }
        });
        lSettingItemAbout.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
            @Override
            public void click() {
                Intent intent = new Intent(context, AboutActivity.class);
                context.startActivity(intent);
            }
        });
        lSettingItemHistoryUserInfor.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
            @Override
            public void click() {
                Intent intent =new Intent(context,PanssegerInformationActivity.class);
                context.startActivity(intent);
            }
        });

        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = getContext();
                Intent intent = new Intent(context, LoginActivity.class);
                context.startActivity(intent);
            }
        });

    }


    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
